package code.web.controller;

import cn.hutool.core.util.StrUtil;
import code.service.IHistoryService;
import code.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import code.dto.ResultDto;
import code.model.History;

@Controller
@RequestMapping("history")
@Api(value = "历史相关接口", tags = {"历史相关接口"})
public class HistoryController {


    @Autowired
    private IHistoryService historyService;

    @ResponseBody
    @RequestMapping(value = "/addHistory", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "添加一条记录接口")
    public ResultDto<History> addOne(@RequestBody History history) {
        History add = historyService.addHistory(history);
        if (add != null) {
            return ResultDto.ok(add);
        }
        return ResultDto.failed();
    }


    @ResponseBody
    @RequestMapping(value = "/listHistory", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "分页条件查询记录接口")
    public ResultDto<PageVo<History>> listHistory(
            @RequestParam(required = false, defaultValue = "1") int pageNumber
            , @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        PageVo<History> page = historyService.listHistory(pageNumber, pageSize);
        return ResultDto.ok(page);
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "删除一条记录接口")
    public ResultDto<String> delete(@RequestParam @ApiParam(value = "主键") String id) {
        String delete = historyService.delete(id);
        if (StrUtil.isNotBlank(delete)) {
            return ResultDto.ok(delete);
        }
        return ResultDto.failed();
    }
}
